import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
import '../models/server_config.dart';
import '../models/subscription.dart';

class StorageService {
  static const String _serversKey = 'servers';
  static const String _subscriptionsKey = 'subscriptions';
  static const String _settingsKey = 'settings';

  Future<List<ServerConfig>> loadServers() async {
    try {
      final prefs = await SharedPreferences.getInstance();
      final String? serversJson = prefs.getString(_serversKey);
      
      if (serversJson == null) {
        return [];
      }

      final List<dynamic> decoded = json.decode(serversJson);
      return decoded.map((e) => ServerConfig.fromJson(e)).toList();
    } catch (e) {
      print('Error loading servers: $e');
      return [];
    }
  }

  Future<void> saveServers(List<ServerConfig> servers) async {
    try {
      final prefs = await SharedPreferences.getInstance();
      final String encoded = json.encode(servers.map((e) => e.toJson()).toList());
      await prefs.setString(_serversKey, encoded);
    } catch (e) {
      print('Error saving servers: $e');
    }
  }

  Future<List<Subscription>> loadSubscriptions() async {
    try {
      final prefs = await SharedPreferences.getInstance();
      final String? subscriptionsJson = prefs.getString(_subscriptionsKey);
      
      if (subscriptionsJson == null) {
        return [];
      }

      final List<dynamic> decoded = json.decode(subscriptionsJson);
      return decoded.map((e) => Subscription.fromJson(e)).toList();
    } catch (e) {
      print('Error loading subscriptions: $e');
      return [];
    }
  }

  Future<void> saveSubscriptions(List<Subscription> subscriptions) async {
    try {
      final prefs = await SharedPreferences.getInstance();
      final String encoded = json.encode(subscriptions.map((e) => e.toJson()).toList());
      await prefs.setString(_subscriptionsKey, encoded);
    } catch (e) {
      print('Error saving subscriptions: $e');
    }
  }

  Future<Map<String, dynamic>> loadSettings() async {
    try {
      final prefs = await SharedPreferences.getInstance();
      final String? settingsJson = prefs.getString(_settingsKey);
      
      if (settingsJson == null) {
        return _getDefaultSettings();
      }

      return json.decode(settingsJson);
    } catch (e) {
      print('Error loading settings: $e');
      return _getDefaultSettings();
    }
  }

  Future<void> saveSettings(Map<String, dynamic> settings) async {
    try {
      final prefs = await SharedPreferences.getInstance();
      final String encoded = json.encode(settings);
      await prefs.setString(_settingsKey, encoded);
    } catch (e) {
      print('Error saving settings: $e');
    }
  }

  Map<String, dynamic> _getDefaultSettings() {
    return {
      'autoConnect': false,
      'autoUpdate': false,
      'updateInterval': 24,
      'systemProxy': true,
      'allowInsecure': false,
      'mux': true,
      'theme': 'system',
      'language': 'en',
    };
  }
}
